<template>
  <div class="">
    <div class="tabs_form">
      <el-form :inline="true" class="selectLabel">
        <el-form-item label="会员类型">
          <el-select
            v-model="queryReq.accountType"
            placeholder="请选择会员类型"
            class="tabs_select"
            clearable
          >
            <el-option
              v-for="item in accountTypeParams"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              default-first-option
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效时间">
          <el-date-picker
            v-model="queryReq.dateTime"
            type="daterange"
            align="right"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tabs_button">
      <el-button type="primary" @click="createVipAccount" class="buttonLabel"
        >创建会员账号</el-button
      >
    </div>
    <div class="tabs_table">
      <el-table :data="tableData" border style="width: 100%" max-height="600">
        <el-table-column
          prop="serialNo"
          label="序号"
          align="center"
          type="index"
          width="60"
        >
        </el-table-column>
        <el-table-column
          prop="accountType"
          label="类型"
          align="center"
          :formatter="formatterAccountType"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="content"
          label="内容"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="validTime"
          label="有效时间"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="address"
          label="地址"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="accountNo"
          label="账号"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="password"
          label="密码"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300" align="center">
          <template v-slot="scope">
            <el-button
              @click="handleEdit(scope.$index, scope.row)"
              type="primary"
              plain
              >编辑</el-button
            >
            <el-button type="danger" @click="deleteData(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <DataEdit
    ref="dataEditRef"
    @dataEditConfig="dataEditConfig"
    :dataEditVisible="dataEditVisible"
    :isEdit="isEdit"
  />
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from 'vue'
import { accountRows, memberQueryList, memberDelete } from '@/api/expenses'
import { selectDetail } from '@/constants/commonSelect'
import { getGroupDetail } from '@/api/config'
import { getDateTime } from '@/components/util/dateTimeUtil'
import { ElMessage, ElMessageBox } from 'element-plus'
import DataEdit from './dataEdit.vue'

// 初始化查询
onMounted(() => {
  query()
  getAccountType()
  queryReq.dateTime = getDateTime()
})

let queryReq: any = reactive({
  dateTime: [''],
  startTime: '',
  endTime: '',
  accountType: '',
})

let tableData = ref<accountRows[]>([])

const accountTypeParams = ref<selectDetail[]>([])
const dataEditRef = ref<any>(null)
const isEdit = ref<number>(0)
const dataEditVisible = ref<boolean>(false)

const getAccountType = async () => {
  getGroupDetail('constgroup_account_type').then((res: any) => {
    accountTypeParams.value = res
  })
}

const query = () => {
  console.log('query')
  memberQueryList(queryReq).then((res) => {
    tableData.value = res.data
  })
}

const formatterAccountType = (row: accountRows) => {
  let label = ''
  accountTypeParams.value.forEach((item: any) => {
    if (item.value === row.accountType) {
      label = item.label
    }
  })
  return label
}

const createVipAccount = (index: number, row: any) => {
  console.log('createVipAccount')
  dataEditVisible.value = true
  isEdit.value = 0
  // 调用子组件的 initsObj 方法
  dataEditRef.value?.initsObj(row)
}

const handleEdit = (index: number, row: any) => {
  console.log('handleEdit')
  dataEditVisible.value = true
  isEdit.value = 1
  // 调用子组件的 initsObj 方法
  dataEditRef.value?.initsObj(row)
}

const deleteData = (row: any) => {
  ElMessageBox.confirm('你确认删除这条数据吗?', 'Warning', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      memberDelete(row.id).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功!',
        })
        query()
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}

const dataEditConfig = (status: any) => {
  console.log('dataEditConfig')
  dataEditVisible.value = status
  query()
}
</script>

<style lang="scss" scoped>
.tabs_form {
  height: 60px;
  margin-top: 10px;
  position: relative;
}

.tabs_table {
  position: relative;
  margin-top: 10px;
}

.tabs_select {
  top: 0px;
  width: 200px;
}

.tabs_button {
  position: absolute;
  left: 20px;
  top: 10px;
}

.selectLabel {
  position: absolute;
  right: 10px;
}

.buttonLabel {
  margin-left: 20px;
  margin-bottom: 40px;
}

.countShowClass {
  margin-top: 10px;
  margin-left: 10px;
  margin-bottom: 10px;
  font-size: 16px;
  font-weight: bold;
  color: black;
}
</style>
